package com.dwyinyuan.infrastructure.config;

import com.github.benmanes.caffeine.cache.*;
import lombok.extern.slf4j.*;
import org.springframework.cache.*;
import org.springframework.cache.annotation.*;
import org.springframework.cache.caffeine.*;
import org.springframework.cache.support.*;
import org.springframework.context.annotation.*;

import java.util.*;
import java.util.concurrent.*;


/**
 * 微信公众号：笛舞音缘
 * <p>
 * 缓存管理器配置类
 * <p>
 * Created by andy  on 2020-12-16.
 */
@Configuration
@EnableCaching
@Slf4j
public class CaffeineCacheConfig {

    /**
     * CacheManager实现类
     *
     * @return
     */
    @Bean("cacheManager")
    public CacheManager cacheManager() {
        SimpleCacheManager cacheManager = new SimpleCacheManager();

        // 缓存集合
        ArrayList<CaffeineCache> caches = new ArrayList<>();

        // 对缓存key属性做设置
        caches.add(new CaffeineCache("users-cache",
                Caffeine.newBuilder()
                        // 指定Key下的最大缓存数据量
                        .maximumSize(1000)
                        // 最后一次访问之后 120秒 过期
                        .expireAfterAccess(120, TimeUnit.SECONDS)
                        .build()));

        cacheManager.setCaches(caches);

        return cacheManager;
    }

}
